﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class DanhMucTinTuc_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public DanhMucTinTuc_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<DanhMucTinTuc> getListBy(string whereCom, string orderCom)
        {
            List<DanhMucTinTuc> data = new List<DanhMucTinTuc>();
            string comStr = "Select * from [DanhMucTinTuc]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        DanhMucTinTuc obj = new DanhMucTinTuc();

                        obj.MaDanhMuc = Convert.ToInt32(dap.DaReader["MaDanhMuc"]);
                        obj.TenDanhMuc = dap.DaReader["TenDanhMuc"].ToString();
                        obj.GhiChu = dap.DaReader["GhiChu"].ToString();                                            

                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<DanhMucTinTuc> getListByKey()
        {
            return getListBy("", " [MaDanhMuc] asc");
        }
    }
}